﻿
//仓位明细

var mStorage = {};
var mCtlType;

$(function () {

    try {
        var args = getUrlArgs();
        var storageId = 0;

        storageId = args["storageId"];
        mCtlType = args["ctrType"];


        $("#cmbDept").combobox({
            valueField: 'Id',
            textField: 'Name',
            panelHeight: 160,
            onSelect: deptSelected //设置管理部门
        });

        loadOrganizatonList();

        loadStorage(storageId);
        displayData();

    } catch (ex) {
        alert(ex);
    }
});

function loadStorage(storageId) {

    if (storageId == 0) {//表示新增根节点
        mStorage.Parent = null;
        mStorage.IsLeaf = true;
        mStorage.NodeLevel = 1;
    } else if (storageId > 0) {
        var result = {};
        $.ajax({
            url: "/BaseData/LoadStorage",
            type: "POST",
            dataType: "json",
            data: { "storageId": storageId },
            async: false,
            success: function (req) {
                if (req.IsSuccess) {//成功填充数据
                    result = JSON.parse(req.Data);
                }
                else {
                    $.messager.alert('提示', '获取数据失败 ');
                }
            }
        });

        if (mCtlType == 0) {
            mStorage.Parent = result;
            mStorage.IsLeaf = true;
            mStorage.IsExclusive = result.IsExclusive;
            mStorage.NodeLevel = result.NodeLevel + 1;
            mStorage.ManageDept = result.ManageDept;
        } else {
            mStorage = result;
        }

    }

}

function checkIsUsed(storageId) {
    var isUsed = false;

    if (storageId > 0) {
        $.ajax({
            url: "/BaseData/CheckStorageIsUsed",
            type: "POST",
            dataType: "json",
            data: { "storageId": storageId },
            async: false,
            success: function (req) {
                if (req.IsSuccess) {//成功填充数据
                    isUsed = req.Data
                }
                else {
                    $.messager.alert('提示', '获取数据失败 ');
                }
            }
        });
    }
    return isUsed;
}

function displayData() {

    if (mStorage) {

        $("#txtID").val(mStorage.Id);
        $("#txtNumber").val(mStorage.Number);
        $("#txtName").val(mStorage.Name);
        $("#txtLevel").val(mStorage.NodeLevel);
        $("#txtIsLeaf").val(mStorage.IsLeaf ? '是' : '否');
        $("#txtDescription").val(mStorage.Description);
        if (mStorage.IsExclusive)
            $("#chkExclusive").attr("checked", "checked");
        if (mStorage.IsEmpty)
            $("#chkEmpty").attr("checked", "checked");

        if (mStorage.ManageDept) {
            //alert("xxx:" + mStorage.ManageDept.Id);
            $("#cmbDept").combobox("select", mStorage.ManageDept.Id);
        }

        if (mStorage.Parent) {
            $("#txtParentName").val(mStorage.Parent.Name);
        }
        var isUsed = checkIsUsed(mStorage.Id)

        if (isUsed) {
            $("#txtNumber").attr("disabled", true);
            $("#txtName").attr("disabled", true);
            $("#txtDescription").attr("disabled", true);
        }

        //只有根节点才能设置管理部门！
        if (mStorage.NodeLevel == 1 && !isUsed) {
            $("#cmbDept").combobox('enable');
            $("#chkExclusive").attr("disabled", false);
        } else {
            $("#cmbDept").combobox('disable');
            $("#chkExclusive").attr("disabled", true);
        }

    } else {
        $("#txtID").val(0);
    }
}

function checkSorage() {
    if (mStorage.Name == null || mStorage.Name == '') {
        $.messager.alert('提示', '名称不能为空！');
        return false;
    }

    return true;
}

function saveStorage() {

    var isSuccess = false;

    try {
        mStorage.Number = $("#txtNumber").val();
        mStorage.Name = $("#txtName").val();
        mStorage.IsUsed = 1;

        if ($("#chkExclusive").attr("checked") == "checked") {
            mStorage.IsExclusive = true;
        } else {
            mStorage.IsExclusive = false;
        }


        mStorage.Description = $("#txtDescription").val();

        if (checkSorage()) {
            var json = JSON.stringify(mStorage);

            $.ajax({
                url: "/BaseData/SaveStorage",
                type: "POST",
                dataType: "json",
                data:{"storageJson":json},
                async: false,
                success: function (req) {
                    if (req.IsSuccess) {
                        isSuccess = req.IsSuccess;
                        loadStorage(req.Data);
                        displayData();
                        parent.loadStorages();
                    } else {
                        $.messager.alert('提示', req.Data);
                    }
                }
            });
        }

        if (isSuccess) {
            parent.art.dialog.list['diaFormStorage'].close();
        }

    } catch (ex) {
        $.messager.alert('提示', ex);
    }
}


function loadOrganizatonList() {

    gLoadAllDepts(function (orgs) {
        $('#cmbDept').combobox("loadData", orgs);
    });
}

function closeWindow() {
    parent.mStorageViewDlg.close();
    //parent.art.dialog.list['mStorageViewDlg'].close();
}

function deptSelected(record) {
    mStorage.ManageDept = record;
}